matchinitx
A good matcher
Usage
Install the package
pnpm add matchinitx
Import the package
import { type MatcherRules, useInitxMatcher } from 'matchinitx'
Basic usage
interface CustomField {
name: string
}
function resultFn(rule: CustomField, ...others: string[]) {
return { rule, others }
}
const rules: MatcherRules<CustomField> = [
{
matching: [
'foo',
/^bar/
],
name: 'first'
},
{
matching: [
'test',
/^t/
],
name: 'second'
}
]
const matcher = useInitxMatcher(resultFn)
matcher.match(rules, 'foo')
matcher.match(rules, 'barfoo')
matcher.match(rules, 'test')
matcher.match(rules, 'top', 'extra')
Type matcher
interface CustomField {
title: string
}
function resultFn(rule: CustomField, type: string, ...others: string[]) {
return { rule, type, others }
}
enum CustomType {
FOO = 'foo',
BAR = 'bar'
}
const rules: MatcherRules<CustomField> = {
[CustomType.FOO]: {
matching: [
'f',
'o'
],
title: 'a good matcher'
},
[CustomType.BAR]: {
matching: [
'b',
'a'
],
title: 'a good title'
}
}
const matcher = useInitxMatcher(resultFn)
matcher.match(rules, 'f')
matcher.match(rules, 'o')
matcher.match(rules, 'b', 'extra')
matcher.match(rules, 'a', 'extra')